package com.ankiang.treeprint.fragment.components;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.PopupWindow;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ankiang.treeprint.R;
import com.ankiang.treeprint.base.BaseFragment;
import com.ankiang.treeprint.lib.TRDataManager;
import com.ankiang.treeprint.lib.annotation.Widget;
import com.ankiang.treeprint.manager.QDDataManager;
import com.ankiang.treeprint.model.TRItemTree;
import com.ankiang.treeprint.view.TRConfig;
import com.qmuiteam.qmui.alpha.QMUIAlphaImageButton;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView;
import com.qmuiteam.qmui.widget.popup.QMUIListPopup;
import com.qmuiteam.qmui.widget.popup.QMUIPopup;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * {@link QMUIGroupListView} 的使用示例。
 * Created by Kayo on 2016/11/21.
 */

@Widget(widgetClass = TRShowTextFragment.class, iconRes = R.mipmap.icon_grid_group_list_view)
public class TRShowTextFragment extends BaseFragment {

    public static final int OP_SHOW_ITEM_LIST = 0;
    public static final int OP_SHOW_ALL = 1;
    public static final int OP_SHOW_GROUP_LIST = 2;
    public static final int OP_SHOW_MENU_LIST = 3;
    public static final int OP_SHOW_CONFIG_LIST = 4;
    public static final int OP_SHOW_ITEM = 5;
    public static final int OP_SHOW_GROUP = 6;
    @BindView(R.id.topbar)
    QMUITopBarLayout mTopBar;


    // 右上角列表
    private QMUIListPopup mListPopup;
    private QMUIAlphaImageButton mRightButton;

    @BindView(R.id.groupListView)
    QMUIGroupListView mGroupListView;

    String content;

    public void setContent(String content) {
        this.content = content;
    }

    private QDDataManager.QDItemDescription mQDItemDescription;

//    // 保存数据
//    private int itemId;

    int id;

    public void setId(int id) {
        this.id = id;
    }



    // 配置
    private TRConfig conf;

    @BindView(R.id.contentTextView)
    EditText contentTextView;


    TRItemTree mItemTree;

    TRDataManager dataManager;


    int mOp = OP_SHOW_ITEM_LIST;
    int group;

    public void setGroup(int group) {
        this.group = group;
    }

    @Override
    protected View onCreateView() {
        View root = LayoutInflater.from(getActivity()).inflate(R.layout.tr_fragment_show_text, null);
        ButterKnife.bind(this, root);

        conf = TRConfig.getInstance();

        dataManager = TRDataManager.getInstance(getContext());

        initTopBar();
        initData(this.mOp);

        return root;
    }

    private void initData(int op) {


        switch (op) {
            case OP_SHOW_ITEM_LIST:
                mItemTree = dataManager.getData(String.valueOf(group));
                content = JSON.toJSONString(mItemTree.toJSONArray(), true);
                break;
            case OP_SHOW_ITEM:
                // 由外部设置
//                mItemTree = dataManager.getData(String.valueOf(group));
//                content = JSON.toJSONString(mItemTree.get(this.id), true);
                break;
            case OP_SHOW_ALL:
                content = JSON.toJSONString(dataManager.getAllData(), true);
                break;
            case OP_SHOW_GROUP_LIST:
                content = JSON.toJSONString(dataManager.getGroupList(), true);
                break;
            case OP_SHOW_GROUP:
                content = JSON.toJSONString(dataManager.getGroupList().getJSONObject(String.valueOf(this.id)), true);
                break;
            case OP_SHOW_CONFIG_LIST:
                content = JSON.toJSONString(dataManager.getConfigList(), true);
                break;
            case OP_SHOW_MENU_LIST:
                content = JSON.toJSONString(dataManager.getMenuList(), true);
                break;

        }
//        JSON.toJSONString()
        contentTextView.setText(content);


    }


    private void initTopBar() {
        mTopBar.setTitle(V(R.string.show_text));

        mTopBar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popBackStack();
            }
        });


        mRightButton = mTopBar.addRightImageButton(R.mipmap.icon_topbar_overflow, R.id.topbar_right_change_button);
        mRightButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showRightButtonOps();
            }
        });

    }

    // --------------------------------------------------
    // 操作
    private void showRightButtonOps() {

        View v = mRightButton;
        initListPopupIfNeed();
        mListPopup.setAnimStyle(QMUIPopup.ANIM_GROW_FROM_CENTER);
        mListPopup.setPreferredDirection(QMUIPopup.DIRECTION_TOP);
        mListPopup.show(v);

    }


    private void initListPopupIfNeed() {
        if (mListPopup == null) {

            ArrayList<String> listItems = new ArrayList();
            listItems.add(V(R.string.show_text_group_list));
            listItems.add(V(R.string.show_text_menu_list));
            listItems.add(V(R.string.show_text_config_list));
            listItems.add(V(R.string.show_text_all));


            ArrayAdapter adapter = new ArrayAdapter<>(getContext(), R.layout.simple_list_item, listItems);

            mListPopup = new QMUIListPopup(getContext(), QMUIPopup.DIRECTION_NONE, adapter);
            mListPopup.create(QMUIDisplayHelper.dp2px(getContext(), 120), QMUIDisplayHelper.dp2px(getContext(), 500), new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    switch (i) {
                        case 0:
                            initData(OP_SHOW_GROUP_LIST);
                            break;
                        case 1:
                            initData(OP_SHOW_MENU_LIST);
                            break;
                        case 2:
                            initData(OP_SHOW_CONFIG_LIST);
                            break;
                        case 3:
                            initData(OP_SHOW_ALL);
                            break;
                    }
                    mListPopup.dismiss();
                }
            });
            mListPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                }
            });
        }
    }


    public void setOp(int op) {
        this.mOp = op;
    }
}
